package cn.edu.ecut.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;

public class ThirdClient {
	
	private Scanner scanner = new Scanner( System.in );
	
	private String remoteIp = "1.0.0.99" ;
	private int remotePort = 9527 ;
	
	private Socket socket ;
	
	public void init() {
		socket = new Socket();
		SocketAddress remote = new InetSocketAddress( remoteIp , remotePort );
		try {
			socket.connect( remote );
		} catch ( IOException cause ) {
			throw new RuntimeException( "远程主机连接失败" , cause );
		}
	}
	
	public void send() {
		try {
			OutputStream out = socket.getOutputStream(); // 获得可以向服务器输出数据的字节输出流
			PrintStream ps = new PrintStream( out ) ; // 创建一个字节打印流
			while( true ) {
				String msg = scanner.nextLine();
				ps.println( msg );
				if( "byebye".equalsIgnoreCase( msg ) ) {
					break ;
				}
			}
			ps.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws IOException {
		
		ThirdClient tc = new ThirdClient();
		
		tc.init();
		
		tc.send();
		
	}

}
